﻿using Admin.Crypto.Session;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NSubstitute;
using SqlSugar;
using Xunit.Abstractions;

namespace Admin.Crypto.ApplicationTests
{
    public abstract class AbstractTestBase
    {
        protected ITestOutputHelper Output { get; }
        protected IServiceProvider ServiceProvider { get; private set; }
        protected ILogger Logger { get; private set; }

        public AbstractTestBase(ITestOutputHelper outputHelper)
        {
            Output = outputHelper;
            Init();
        }

        private void Init()
        {
            var services = new ServiceCollection();
            var path = $"{AppContext.BaseDirectory}/";
            var builder = new ConfigurationBuilder()
                .SetBasePath(path)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

            var logger = new LoggerFactory().AddLog4Net("config/log4net.config");
            services.AddTransient(typeof(IHttpContextAccessor), x =>
            {
                var rs = Substitute.For<IHttpContextAccessor>();
                rs.HttpContext = new DefaultHttpContext();
                return rs;
            });

            services.AddLogging(logging => logging.AddLog4Net("config/log4net.config"));

            var defaultConnectionString = "server=127.0.0.1;database=study;port=3306;uid=root;pwd=123456;charset=utf8mb4;Convert Zero Datetime=True;SslMode=None;Allow User Variables=True;Connect Timeout=90;AllowLoadLocalInfile=true;";
            var sqlSugar = new SqlSugarScope(new ConnectionConfig
            {
                ConnectionString = defaultConnectionString,
                DbType = DbType.MySql,
                IsAutoCloseConnection = true,
                SlaveConnectionConfigs = new List<SlaveConnectionConfig> {
                                            new SlaveConnectionConfig {
                                                ConnectionString = defaultConnectionString,
                                                HitRate = 100
                                            }
                                        }
            });        

            sqlSugar.DbMaintenance.CreateDatabase();
            sqlSugar.CodeFirst.InitTables(typeof(CustomerContact));
            services.AddSingleton(logger);
            services.AddSingleton<ISqlSugarClient>(sqlSugar); 
            services.AddTransient<ICustomerContactRepository, CustomerContactRepository>();
            services.AddTransient<ICustomerContactAppService, CustomerContactAppService>();            
            services.AddTransient<IUserSession, TestUserSession>();

            ConfigurationService(services);

            ServiceProvider = services.BuildServiceProvider();
            var factory = ServiceProvider.GetService<ILoggerFactory>();
            Logger = factory.CreateLogger(this.GetType());
        }

        public virtual void ConfigurationService(IServiceCollection serives)
        {

        }
    }
}
